<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.ruleCode')" prop="ruleCode">
            <el-input v-model="form.ruleCode" :placeholder="$t('mesBillsRule.enterRuleCode')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.ruleName')" prop="ruleName">
            <el-input v-model="form.ruleName" :placeholder="$t('mesBillsRule.enterRuleName')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.prefix')" prop="prefix">
            <el-input v-model="form.prefix" :placeholder="$t('mesBillsRule.enterPrefix')"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.year')" prop="year">
            <el-radio-group v-model="form.year" class="ml-4">
                <el-radio border label="true">{{ $t('mesBillsRule.enable') }}</el-radio>
                <el-radio border label="false">{{ $t('mesBillsRule.forbidden') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.quarter')" prop="quarter">
            <el-radio-group v-model="form.quarter" class="ml-4">
              <el-radio border label="true">{{ $t('mesBillsRule.enable') }}</el-radio>
              <el-radio border label="false">{{ $t('mesBillsRule.forbidden') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.month')" prop="month">
            <el-radio-group v-model="form.month" class="ml-4">
              <el-radio border label="true">{{ $t('mesBillsRule.enable') }}</el-radio>
              <el-radio border label="false">{{ $t('mesBillsRule.forbidden') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.day')" prop="day">
            <el-radio-group v-model="form.day" class="ml-4">
              <el-radio border label="true">{{ $t('mesBillsRule.enable') }}</el-radio>
              <el-radio border label="false">{{ $t('mesBillsRule.forbidden') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.week')" prop="week">
            <el-radio-group v-model="form.week" class="ml-4">
              <el-radio border label="true">{{ $t('mesBillsRule.enable') }}</el-radio>
              <el-radio border label="false">{{ $t('mesBillsRule.forbidden') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.serialNumberLength')" prop="serialNumberLength">
            <el-input-number :min="2" v-model="form.serialNumberLength"
                             :placeholder="$t('mesBillsRule.enterSerialNumberLength')"></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item :label="$t('mesBillsRule.remark')" prop="remark">
            <el-input v-model="form.remark" :placeholder="$t('mesBillsRule.enterRemark')"/>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{$t('mesBillsRule.cancel')}}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{$t('mesBillsRule.confirm')}}</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MesBillsRuleDialog">
import {useMessage} from "/@/hooks/message";
import {addObj, getObj, putObj} from '/@/api/platform/digital/mesBillsRule'
import {rule} from '/@/utils/validate';
import {useI18n} from 'vue-i18n';
const {t} = useI18n();
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  ruleCode: '',
  ruleName: '',
  prefix: '',
  year: 'false',
  quarter: 'false',
  month: 'false',
  day: 'false',
  week: 'false',
  serialNumberLength: 0,
  remark: '',
});

// 定义校验规则
const dataRules = ref({
  ruleCode: [{required: true, message: t('mesBillsRule.notNullRuleCode'), trigger: 'blur'}, {
    validator: rule.letterAndNumber,
    trigger: 'blur'
  }],
  ruleName: [{required: true, message: t('mesBillsRule.notNullRuleName'), trigger: 'blur'}],
  prefix: [{required: true, message: t('mesBillsRule.notNullPrefix'), trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取mesBillsRule信息
  if (id) {
    form.id = id
    getmesBillsRuleData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('mesBillsRule.editSuccess') : t('mesBillsRule.addSuccess'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getmesBillsRuleData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    if (res && res.data) {
      res.data.serialNumberLength = (res?.data?.serialNumberLength) ? (Number(res?.data?.serialNumberLength)) : undefined
    }
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>